﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Guest_room.admin
{
    public partial class log : Form
    {
        public log()
        {
            InitializeComponent();
        }
        SqlDataAdapter daLog;
        DataSet dl = new DataSet();//适配器对象
        void init()
        {
            DB.GetCn();
            string str = "select * from 日志表";
            daLog = new SqlDataAdapter(str, DB.cn);
            daLog.Fill(dl, "log_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvLog = new DataView(dl.Tables["log_info"]);
            dataGridView1.DataSource = dvLog;
            //列名居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列宽自适应列名
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            //逆序排列
            dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
            //选中行前景色为黑色
            dataGridView1.RowsDefaultCellStyle.SelectionForeColor = Color.Black;
            //选中行背景色为透明
            dataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Transparent;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                try
                {
                    // 检查单元格值是否为 null
                    if (dataGridView1.Rows[i].Cells["操作类型"].Value != null)
                    {
                        string 操作类型 = dataGridView1.Rows[i].Cells["操作类型"].Value.ToString();

                        // 根据操作类型设置行背景色
                        if (操作类型 == "删除")
                        {
                            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightPink;
                        }
                        else if (操作类型 == "修改")
                        {
                            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightYellow;
                        }
                        else if (操作类型 == "添加")
                        {
                            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }

        }

        private void log_Load(object sender, EventArgs e)
        {
            init();
            showAll();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)//刷新
        {
            dl.Clear(); //清空数据集
            init();
            showAll();
        }
    }
}
